package com.qf.controller;

import com.qf.service.CourseService;
import com.qf.service.impl.CourseServiceImpl;
import com.qf.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/course")
public class CourseController extends BaseServlet {
    private CourseService courseService = new CourseServiceImpl();

    /**
     * 获取课程列表
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void doGetCourses(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        courseService.getCourses(request, response);
    }

    /**
     * 根据ID删除课程
     *
     * @param request
     * @param response
     */
    public void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        courseService.delete(request, response);
    }

    /**
     * 添加课程
     *
     * @param request
     * @param response
     */
    public void doAddCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        courseService.add(request, response);
    }

    /**
     * 初始化修改课程信息
     *
     * @param request
     * @param response
     */
    public void doInitModify(HttpServletRequest request, HttpServletResponse response) {
        courseService.initModify(request, response);

        try {
            request.getRequestDispatcher("courseInfo.jsp").forward(request, response);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void getCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        courseService.getCourse(request, response);
    }

    /**
     * 根据课程ID修改课程
     *
     * @param request
     * @param response
     */
    public void doModify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        courseService.modify(request, response);

    }
}